﻿using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace DataAccess
{
    public class DataAccess : MarshalByRefObject
    {
        public Object getFood(string cookID)
        {
            // create and open a connection
            SqlConnection con = new SqlConnection();
            con.ConnectionString = "uid=sa; pwd=haiyang228; Initial Catalog=Demo; Data Source=(local)";
            con.Open();

            // create a sql command object
            string strSql = "select orderitem.id, orderitem.foodID, foodName, notes from menu,orderitem,orderlist "
                    + "where itemState=0 and orderitem.foodID = menu.foodID and orderitem.orderID = orderlist.orderID "
                    + "and cookID in ('0', '" + cookID + "') "
                    + " order by orderTime";
            SqlCommand command = new SqlCommand(strSql, con);

            // call ExecuteReader() to get a data reader
            SqlDataReader sdr = command.ExecuteReader(CommandBehavior.CloseConnection);

            Hashtable showinfo = new Hashtable();
            // circle all record
            if (sdr.Read())
            {
                showinfo.Add("id", sdr["id"].ToString().Trim());
                showinfo.Add("foodID", sdr["foodID"].ToString().Trim());
                showinfo.Add("foodName", sdr["foodName"].ToString().Trim());
                showinfo.Add("notes", sdr["notes"].ToString().Trim());
            }

            sdr.Close();
            return showinfo;
        }

        public bool setComplete(string id)
        {
            try
            {
                SqlConnection con = new SqlConnection();
                con.ConnectionString = "uid=sa; pwd=haiyang228; Initial Catalog=Demo; Data Source=(local)";
                con.Open();

                string strSql = "update orderitem set itemState=1 where id=" + id + ";";
                Console.WriteLine(strSql);
                SqlCommand command = new SqlCommand(strSql, con);
                command.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return false;
            }
            return true;
        }
    }
}
